package com.taplinker.client.im.domain;

import java.io.File;

import android.content.Context;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.im.rpc.Cmd006;
import com.taplinker.core.Resource;
import com.taplinker.core.ResourceManager;

public class ImMessageHandler implements Resource {
	private static ImMessageHandler instance;

	private ImMessageHandler() {
	}

	public static ImMessageHandler getInstance() {
		if (instance != null) {
			return instance;
		}
		synchronized (ImMessageHandler.class) {
			if (instance == null) {
				instance = new ImMessageHandler();
				ResourceManager.getInstance().addResource(instance);
			}
		}
		return instance;
	}

	private void sendMessage(final TapLinkerMessage message) {
		if (message != null) {
			new Cmd006(message).execute();
		}
	}

	/**
	 * 发送消息
	 * 
	 * @param content
	 * @param conversation
	 * @param type
	 */
	public void sendMessage(final String content,
			final TapLinkerConversation conversation, final String type) {
		TapLinkerMessage message = new TapLinkerMessage();
		message.setContent(content);
		message.setType(type);
		message.setConversationId(conversation.getId());
		message.setSendUserId(AppConfigFactory.getInstance().getUserInfo()
				.getUserid());
		message.setSendUser(AppConfigFactory.getInstance().getUserInfo()
				.getNickname());
		message.setIcon(AppConfigFactory.getInstance().getUserInfo()
				.getUserIcon());
		conversation.addMessage(message);
		sendMessage(message);
	}

	/**
	 * 
	 * @param message
	 * @param conversationId
	 */
	// public void sendMessage(final TapLinkerMessage message, final String
	// conversationId) {
	// message
	// }

	/**
	 * 发送文本消息
	 * 
	 * @param content
	 * @param conversation
	 */
	public void sendTextMessage(final String content,
			final TapLinkerConversation conversation) {
		TapLinkerMessage message = new TapLinkerMessage();
		message.setContent(content);
		message.setType(EnumMessageType.TEXT.getType());
		message.setConversationId(conversation.getId());
		message.setSendUserId(AppConfigFactory.getInstance().getUserInfo()
				.getUserid());
		message.setSendUser(AppConfigFactory.getInstance().getUserInfo()
				.getNickname());
		message.setIcon(AppConfigFactory.getInstance().getUserInfo()
				.getUserIcon());
		conversation.addMessage(message);
		sendMessage(message);
	}

	/**
	 * 发送图片
	 * 
	 * @param file
	 * @param conversation
	 */
	public void sendImageMessage(final File file,
			final TapLinkerConversation conversation) {
		final TapLinkerMessage message = new TapLinkerMessage();
		message.setType(EnumMessageType.IMAGE.getType());
		message.setConversationId(conversation.getId());
		message.setSendUserId(AppConfigFactory.getInstance().getUserInfo()
				.getUserid());
		message.setSendUser(AppConfigFactory.getInstance().getUserInfo()
				.getNickname());
		message.setIcon(AppConfigFactory.getInstance().getUserInfo()
				.getUserIcon());
		message.setLocalUrl(file.getPath());
		conversation.addMessage(message);

		ImageManager.getInstance().uploadImage(file, null,
				new ImageManager.LocalCallBack() {

					@Override
					public void callBack(Object obj) {
						message.setContent(obj.toString());
						sendMessage(message);
					}

					@Override
					public void no() {
						message.setUnarrived(true);
						ImDataCenter.getInstance().noticeListener(
								message.getConversationId());
					}

					@Override
					public void updateProgres(long planSize, long curSize) {
						int per = (int) (((float) curSize / (float) planSize) * 100);
						message.setLoadPerecent(per);
					}
				});
	}

	/**
	 * 发送录音
	 * 
	 * @param context
	 * @param file
	 * @param conversation
	 */
	public void sendRecordMessage(Context context, final File file,
			final TapLinkerConversation conversation) {
		final TapLinkerMessage message = new TapLinkerMessage();
		message.setType(EnumMessageType.AUDIO.getType());
		message.setConversationId(conversation.getId());
		message.setLocalUrl(file.getPath());
		message.setSendUserId(AppConfigFactory.getInstance().getUserInfo()
				.getUserid());
		message.setSendUser(AppConfigFactory.getInstance().getUserInfo()
				.getNickname());
		message.setIcon(AppConfigFactory.getInstance().getUserInfo()
				.getUserIcon());
		conversation.addMessage(message);

	}

	@Override
	public void clear() {

	}

	@Override
	public void releaseResource() {

	}

}
